package us.mooresr.geartrack.client.core.presenter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.annotations.NameToken;

import us.mooresr.geartrack.client.events.ItemCheckedOutEvent;
import us.mooresr.geartrack.client.events.ItemDeletedEvent;
import us.mooresr.geartrack.client.place.NameTokens;
import us.mooresr.geartrack.shared.action.CheckOutItemAction;
import us.mooresr.geartrack.shared.action.CheckOutItemResult;
import us.mooresr.geartrack.shared.action.GetAllAvailableItemsAction;
import us.mooresr.geartrack.shared.action.GetAllAvailableItemsResult;
import us.mooresr.geartrack.shared.action.GetAllItemsAction;
import us.mooresr.geartrack.shared.action.GetAllItemsResult;
import us.mooresr.geartrack.shared.action.GetAllPeopleAction;
import us.mooresr.geartrack.shared.action.GetAllPeopleResult;
import us.mooresr.geartrack.shared.model.Person;
import us.mooresr.geartrack.shared.model.Item;

import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;

public class BorrowPresenter extends
		Presenter<BorrowPresenter.MyView, BorrowPresenter.MyProxy> 
		implements BorrowUiHandlers {
	
	private final DispatchAsync dispatcher;

	public interface MyView extends View, HasUiHandlers<BorrowUiHandlers> {
		public void setItems(List<Item> list);
		public void setPeople(List<Person> people);
		public void removeItem(Item item);
	}

	@ProxyStandard
	@NameToken(NameTokens.borrow)
	public interface MyProxy extends ProxyPlace<BorrowPresenter> {
	}

	@Inject
	public BorrowPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy, DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		getView().setUiHandlers(this);
		
		this.dispatcher = dispatcher;
		

		
	}


	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, MainPresenter.TYPE_SetMainContent, this);
	}
	
	@Override
	public void onBind() {
		super.onBind();
		
		addRegisteredHandler(ItemCheckedOutEvent.getType(), new ItemCheckedOutEvent.ItemCheckedOutHandler() {
			
			@Override
			public void onItemCheckedOut(ItemCheckedOutEvent event) {
				getView().removeItem(event.getItem());
				
			}

		});
	}
	
	@Override
	public void onReveal() {
		super.onReveal();
		
		// Launch a request to get the item list data
		GetAllAvailableItemsAction action = new GetAllAvailableItemsAction();
		
		dispatcher.execute(action,  new AsyncCallback<GetAllAvailableItemsResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}


			@Override
			public void onSuccess(GetAllAvailableItemsResult result) {
				getView().setItems(result.getItems());
				
			}
			
		});
		
		// Launch a request to fill in the list of people
		GetAllPeopleAction action2 = new GetAllPeopleAction();
		
		dispatcher.execute(action2, new AsyncCallback<GetAllPeopleResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GetAllPeopleResult result) {
				getView().setPeople(result.getPeople());
				
			}
			
		});
	}

	/**
	 * Called by the view when when the view wants to indicate that an item has been borrowed
	 */
	@Override
	public void borrowItem(Long itemId, Long personId) {
		// Tell the model that the item is borrowed
		CheckOutItemAction action = new CheckOutItemAction(itemId, personId);
		
		dispatcher.execute(action, new AsyncCallback<CheckOutItemResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(CheckOutItemResult result) {
				ItemCheckedOutEvent.fire(getEventBus(), result.getItemOut());
				
			}
			
		});

		
	}
}
